package com.venue.hjt.venueclass;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;


import com.venue.hjt.venueclass.db.DaoMaster;
import com.venue.hjt.venueclass.db.DaoSession;
import com.venue.hjt.venueclass.ui.Main2Activity;
import com.venue.hjt.venueclass.utlis.Constant;

import java.io.File;



/**
 * Created by xqx on 2017/10/26.
 *
 */

public class BaseApplication extends MultiDexApplication {

    private static DaoSession daoSession;
    // 用于检测人脸。
    private static String strLibraryName = "opencv_java3"; // 不需要添加前缀 libopencv_java3
    static {
        try {
            Log.e("loadLibrary", strLibraryName);
            System.loadLibrary(strLibraryName);
            //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // couldn't find "libopencv_java320.so"
        } catch (UnsatisfiedLinkError e) {
            Log.e("loadLibrary", "Native code library failed to load.\n" + e);
        } catch (Exception e) {
            Log.e("loadLibrary", "Exception: " + e);
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
//        Fabric.with(this, new Crashlytics());
        Thread.setDefaultUncaughtExceptionHandler(restartHandler);
//        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
//        int memClass = activityManager.getLargeMemoryClass();
//        Log.e("ATG",memClass+" 最大");
//        Fabric.with(this, new Crashlytics());
//
//        try {
//            PrefUtild.INSTANCE.setString(this, Constant.DAYID, "0");
//            PrefUtild.INSTANCE.setString(this, Constant.NPACKAGE, APPUtlis.packageMas(this));
//            PrefUtild.INSTANCE.setString(this, Constant.CODE, APPUtlis.packageCode(this)+"");
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }

        //配置数据库
        setupDatabase();
    }
    // 创建服务用于捕获崩溃异常
    private Thread.UncaughtExceptionHandler restartHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            Log.e("ATG","错误  "+ex.toString());
            restartApp();//发生崩溃异常时,重启应用
            Log.e("MyUncaughtHandler", ex.getMessage(), ex);

        }
    };
    public void restartApp(){
        Intent intent = new Intent(getApplicationContext(),Main2Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
    }
    /**
     * 配置数据库
     */
    private void setupDatabase() {
        File destDir=new File(Environment.getExternalStorageDirectory().getPath()+"/Class");
        File dest=new File(Environment.getExternalStorageDirectory().getPath()+"/Face");
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        if (!dest.exists()){
            dest.mkdirs();
        }
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, Environment.getExternalStorageDirectory().getPath()+"/Class/VenueClass"+ Constant.SNO+".db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }
    public static DaoSession getDaoInstant() {
        return daoSession;
    }
}